package fatworm.metadata;

import java.util.*;

import fatworm.query.constant.*;
import fatworm.server.*;
import fatworm.storage.table.*;
import fatworm.storage.types.*;
import fatworm.tx.*;

public class TableMgr
{
    public static final int MAX_NAME = 16;
    private TableInfo tcatInfo, fcatInfo;

    public TableMgr(boolean isNew, Transaction tx)
    {
        Schema tcatSchema = new Schema();
        tcatSchema.addField("tblname", Types.VARCHAR, MAX_NAME);
        tcatSchema.addField("reclength", Types.INT, -1);
        tcatInfo = new TableInfo(Fatworm.getDBName(), "tblcat", tcatSchema);
        Schema fcatSchema = new Schema();
        fcatSchema.addField("tblname", Types.VARCHAR, MAX_NAME);
        fcatSchema.addField("fldname", Types.VARCHAR, MAX_NAME);
        fcatSchema.addField("type", Types.INT, -1);
        fcatSchema.addField("length", Types.INT, -1);
        fcatSchema.addField("offset", Types.INT, -1);
        fcatInfo = new TableInfo(Fatworm.getDBName(), "fldcat", fcatSchema);
        if (isNew)
        {   
            createTable("tblcat", tcatSchema, tx);
            createTable("fldcat", fcatSchema, tx);
        }
    }
    public void createTable(String tblname, Schema sch, Transaction tx)
    {
        TableInfo ti = new TableInfo(Fatworm.getDBName(), tblname, sch);
        HeapFile tcatfile = new HeapFile(tcatInfo, tx);
        tcatfile.insert();
        tcatfile.setValue("tblname", new StringConstant(tblname));
        tcatfile.setValue("reclength", new IntConstant(ti.tupleLength()));
        tcatfile.close();
        
        HeapFile fcatfile = new HeapFile(fcatInfo, tx);
        for (String fldname : sch.fields())
        {
            fcatfile.insert();
            fcatfile.setValue("tblname", new StringConstant(tblname));
            fcatfile.setValue("fldname", new StringConstant(fldname));
            fcatfile.setValue("type", new IntConstant(sch.type(fldname).getType())); 
            fcatfile.setValue("length", new IntConstant(sch.type(fldname).getM()));
            fcatfile.setValue("offset", new IntConstant(ti.offset(fldname)));
        }
        fcatfile.close();
    }
    public TableInfo getTableInfo(String tblname, Transaction tx)
    {
        HeapFile tcatfile = new HeapFile(tcatInfo, tx);
        int reclen = -1;
        while (tcatfile.next())
            if (tcatfile.getValue("tblname").toString().equals(tblname))
            {
                reclen = (Integer) tcatfile.getValue("reclength").asJavaVal();
                break;
            }
        tcatfile.close();
        HeapFile fcatfile = new HeapFile(fcatInfo, tx);
        Schema sch = new Schema();
        HashMap<String, Integer> offsets = new HashMap<String, Integer>();
        while (fcatfile.next())
            if (fcatfile.getValue("tblname").toString().equals(tblname))
            {
                String fldname = fcatfile.getValue("fldname").toString();
                int fldtype = (Integer) fcatfile.getValue("type").asJavaVal();
                int fldlen = (Integer) fcatfile.getValue("length").asJavaVal();
                int offset = (Integer) fcatfile.getValue("offset").asJavaVal();
                offsets.put(fldname, offset);
                sch.addField(fldname, fldtype, fldlen);
            }
        fcatfile.close();
        return new TableInfo(Fatworm.getDBName(), tblname, sch, offsets, reclen);
    }
}